<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<html>
<head>
<title>菜单管理</title>
<%@ include file="/common/meta.jsp"%>
<SCRIPT type="text/javascript">
<!--
	var resourceTreeObj = null;
	var zNodes =${resourceJson};
	var setting = {
		view: {
			addHoverDom: addHoverDom,
			removeHoverDom: removeHoverDom,
			selectedMulti: false
		},
		edit: {
			enable: true,
			showRemoveBtn:false,
			showRenameBtn:false,
			drag: {
				isCopy: false
			}
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			beforeDrop: resourceTreeBeforeDrop
		}
	};
	
	function addHoverDom(treeId, treeNode) {
		if (treeNode.editNameFlag || $("#addBtn_"+treeNode.id).length>0 || 
				$("#removeBtn_"+treeNode.id).length>0 || $("#editBtn_"+treeNode.id).length>0){
			return;
		}
		var sObj = $("#" + treeNode.tId + "_span");
		var addStr = "<button type='button' class='add' id='addBtn_" + treeNode.id
			+ "' title='新增菜单' onfocus='this.blur();'></button>&nbsp;";
		var editStr = "<button type='button' class='edit' id='editBtn_" + treeNode.id
			+ "' title='修改菜单' onfocus='this.blur();'></button>";
		sObj.append(addStr);
		sObj.append(editStr);
		var btn = $("#addBtn_"+treeNode.id);
		if(btn){
			btn.bind("click", function(){addResource(treeId, treeNode);});
		}
		btn = $("#editBtn_"+treeNode.id);
		if(btn){
			btn.bind("click", function(){editResource(treeId, treeNode);});
		}
	};
	function removeHoverDom(treeId, treeNode) {
		$("#addBtn_"+treeNode.id).unbind().remove();
		$("#editBtn_"+treeNode.id).unbind().remove();
	};
	
	function resourceTreeBeforeDrop(treeId, treeNodes, targetNode, moveType) {
		var tnFrom = treeNodes[0].id;
		var tnFromPid = treeNodes[0].pId;
		var tnTo = targetNode.id;
		var tnToPid = targetNode.pId;
		var urlpath = $("#context_path").val() + "/user/resources!modifyResourceSort.action";
		var ajaxPara = tnFrom + "," + tnFromPid + "," + tnTo + "," + tnToPid + "," + moveType;
		$.post(urlpath, { "ajaxPara":ajaxPara},
				   function(data){
				     if(data == "ok"){
				    	 return true;
				     }
				     return false;
				   }, "text");
		return true;
	};
	
	function treeNodeToForm(treeNode) {
		$("#r_name_m").val(treeNode.name);
		var r_url = treeNode.fromAction;
		if(r_url != null){
			var url_index = r_url.indexOf("?categoryId=");
			if(url_index != -1){
				r_url = r_url.substring(0,url_index);
			}
		}
		$("#r_url_m").val(r_url);
		var r_dataStatus = treeNode.dataStatus;
		if(r_dataStatus == 0){
			$("#r_dataStatus_m_0").attr("checked","checked");
		}else{
			$("#r_dataStatus_m_1").attr("checked","checked");
		}
		$("#r_id_m").val(treeNode.id);		

		$("#r_pid_a").val(treeNode.id);
		$("#r_name_p_a").html(treeNode.name);
	};
	
	var addFormHtml;
	var editFormHtml;
	$(document).ready(function(){
		addFormHtml = $("#addFormDiv").html();
		$("#addFormDiv").html("");
		editFormHtml = $("#modifyFormDiv").html();
		$("#modifyFormDiv").html("");
		resourceTreeObj = $.fn.zTree.init($("#resourceTree"), setting, zNodes);
		resourceTreeObj.expandAll(true);
	});

	function addResource(treeId, treeNode){
		art.dialog({
		    id: 'dialog-add',
		    title: '新增下级菜单',
		    width: 500,
		    height: 150,
		    content: addFormHtml,
		    lock: true,
		    fixed: true
		});
		treeNodeToForm(treeNode);
	};

	function editResource(treeId, treeNode){
		art.dialog({
		    id: 'dialog-edit',
		    title: '修改菜单',
		    width: 500,
		    height: 150,
		    content: editFormHtml,
		    lock: true,
		    fixed: true
		});
		treeNodeToForm(treeNode);
	};
	//-->
	</SCRIPT>
</head>

<body>
<%@ include file="/common/center-top.jsp"%>	
<!-- 网页主显示区，填写表单 -->
<input type="hidden" id="context_path" value="${ctp}"/>
<table class="tableFrom" align="center" cellpadding="0" cellspacing="1">
	<tr>
		<td class="tdInput" style="width:98%" valign="top">
			<ul id="resourceTree" class="ztree"></ul>
		</td>
	</tr>
</table>
<div id="addFormDiv" style="display:none;width:480px;">
	<s:form action="/user/resources!addResource.action" name="addForm" id="addForm">
		<table class="tableFrom" style="width:480px;" align="center" cellpadding="0" cellspacing="1" width="350">
			<tr>
				<td class="tdLabel">父目录名称</td>
				<td class="tdInput">
					<span id="r_name_p_a"></span>
					<input type="hidden" id="r_pid_a" name="resources.pid"/>
				</td>
			</tr>
			<tr>
				<td class="tdLabel">名称</td>
				<td class="tdInput"><s:textfield name="resources.name" cssStyle="width:98%"></s:textfield></td>
			</tr>
			<tr>
				<td class="tdLabel">链接</td>
				<td class="tdInput"><s:textfield name="resources.url" cssStyle="width:98%"></s:textfield></td>
			</tr>
			<tr>
				<td class="tdLabel">排序</td>
				<td class="tdInput"><s:textfield name="resources.sort" cssStyle="width:98%"></s:textfield></td>
			</tr>
		</table>
		<div style="width:100%;height:25;text-align: center;margin-top:5px;">
			<img src="${ctp}/images/add.gif" onclick="addForm.submit()" style="cursor: pointer;"/>
		</div>
	</s:form>
</div>
<div id="modifyFormDiv" style="display:none;">
	<s:form action="/user/resources!modifyResource.action" name="modifyForm" id="modifyForm">
		<table class="tableFrom" style="width:480px;" align="center" cellpadding="0" cellspacing="1">
			<tr>
				<td class="tdLabel">名称</td>
				<td class="tdInput"><s:textfield id="r_name_m" name="resources.name" cssStyle="width:98%"></s:textfield></td>
			</tr>
			<tr>
				<td class="tdLabel">链接</td>
				<td class="tdInput"><s:textfield id="r_url_m" name="resources.url" cssStyle="width:98%"></s:textfield></td>
			</tr>
			<tr>
				<td class="tdLabel">是否可用</td>
				<td class="tdInput">
					<input type="radio" id="r_dataStatus_m_0" name="resources.dataStatus" value="0" />可用
   					<input type="radio" id="r_dataStatus_m_1" name="resources.dataStatus" value="1" />不可用
   				</td>
			</tr>
		</table>
		<input type="hidden" id="r_id_m" name="resources.id"/>
		<div style="width:100%;height:25;text-align: center;margin-top:5px;">
			<img src="${ctp}/images/xg.gif" onclick="modifyForm.submit();" style="cursor: pointer;"/>
		</div>
	</s:form>
</div>
<%@ include file="/common/center-down.jsp"%>				
</body>
</html>
